package com.fullmail;

import java.util.ArrayList;
import java.util.List;

import com.fullmail.adapter.MineCardViewPagerAdapter;
import com.fullmail.fragment.ReceivedFragment;
import com.fullmail.fragment.SendedFragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class MineCard extends FragmentActivity implements OnClickListener{
	private MineCardViewPagerAdapter cardAdapter;
	private ViewPager cardPager;
	private RelativeLayout RLback;
	private ReceivedFragment rf;
	private SendedFragment sf;
	private List<Fragment> flist;
	private RelativeLayout RlReceive;
	private RelativeLayout RlSend;
	private TextView TvReceive;
	private TextView TvSend;
	private int item;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mine_card);
		getExtra();
		initView();
		setListener();
	}

	private void getExtra() {
		// TODO Auto-generated method stub
		item=getIntent().getIntExtra("selectItem", 0);
	}

	private void setListener() {
		// TODO Auto-generated method stub
		RLback.setOnClickListener(this);
		RlReceive.setOnClickListener(this);
		RlSend.setOnClickListener(this);
		cardPager.addOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				// TODO Auto-generated method stub
				setSelectItem(arg0);
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
	}

	private void initView() {
		// TODO Auto-generated method stub
		flist=new ArrayList<Fragment>();
		RLback=(RelativeLayout)findViewById(R.id.rl_card_return);
		RlReceive=(RelativeLayout)findViewById(R.id.rl_card_list);
		RlSend=(RelativeLayout)findViewById(R.id.rl_card_calendar);
		TvReceive=(TextView)findViewById(R.id.tv_card_list);
		TvSend=(TextView)findViewById(R.id.tv_card_calendar);
		cardPager=(ViewPager)findViewById(R.id.vp_cards);
		rf=new ReceivedFragment();
		sf=new SendedFragment();
		flist.add(rf);
		flist.add(sf);
		cardAdapter=new MineCardViewPagerAdapter(flist, getSupportFragmentManager());
		cardPager.setAdapter(cardAdapter);
		cardPager.setCurrentItem(item);
		setSelectItem(item);
	}
	private void setSelectItem(int count){
		switch(count){
		case 0:
			setAllColor();
			RlReceive.setBackgroundColor(getResources().getColor(R.color.white));
			TvReceive.setTextColor(getResources().getColor(R.color.theme_pink));
			break;
		case 1:
			setAllColor();
			RlSend.setBackgroundColor(getResources().getColor(R.color.white));
			TvSend.setTextColor(getResources().getColor(R.color.theme_pink));
			break;
		}
	}
	
	private void setAllColor(){
		RlReceive.setBackgroundColor(getResources().getColor(R.color.theme_pink));
		RlSend.setBackgroundColor(getResources().getColor(R.color.theme_pink));
		TvReceive.setTextColor(getResources().getColor(R.color.white));
		TvSend.setTextColor(getResources().getColor(R.color.white));
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.rl_card_return){
			finish();
		}
		if(v.getId()==R.id.rl_card_list){
			cardPager.setCurrentItem(0);
		}
		if(v.getId()==R.id.rl_card_calendar){
			cardPager.setCurrentItem(1);
		}
	}
}
